"""
---------------------------------------------------------------------
- File: router.py
- Date: 2025/8/8
- Author: yangzhide
- Email: xxzjyzd@126.com
- Description: swagger UI 本地资源
---------------------------------------------------------------------
"""

from fastapi import FastAPI, APIRouter
from fastapi.openapi.docs import get_swagger_ui_html, get_redoc_html
from starlette.responses import FileResponse

app =FastAPI()
router = APIRouter(tags=["swagger"], include_in_schema=False)

@router.get("/static/swagger/favicon-{size}.png")
async def get_favicon(size:str):
    """单独处理图片加载"""
    return FileResponse(f"static/swagger/favicon-{size}x{size}.png")

@router.get("/docs", description="无网络的情况下，使用本地Swagger-ui")
async def swagger_ui_html():
    return get_swagger_ui_html(
        openapi_url="/openapi.json",
        title="API Docs",
        swagger_js_url="/static/swagger/swagger-ui-bundle.js",
        swagger_css_url="/static/swagger/swagger-ui.css",
        swagger_favicon_url="/static/swagger/favicon-32.png",
    )

@router.get("/redoc", description="无网络的情况下，使用本地Swagger-ui")
async def redoc_html():
    return get_redoc_html(
        openapi_url="/openapi.json",
        title="API ReDoc",
        redoc_js_url="/static/swagger/redoc.standalone.js",
        redoc_favicon_url="/static/swagger/favicon-32.png",
    )

